package com.example.mbl.service.impl;

import com.example.mbl.dao.PatentMapper;
import com.example.mbl.entity.Patent;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.WebRestResult;
import com.example.mbl.result.business.PatentPageResult;
import com.example.mbl.service.PatentService;
import com.example.mbl.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PatentServiceImpl implements PatentService {
    @Autowired
    private PatentMapper patentMapper;

    @Override
    public List<Patent> searchItemsByKeyWord(String keyword) {
        return patentMapper.searchItemsByKeyWord(keyword);
    }

    @Override
    public WebRestResult addPatent(Patent patent) {
        WebRestResult result = new WebRestResult();
        patent.setCreatedate(new Date());
        patent.setPatentid(UUIDUtil.randomUUID());
        if(patentMapper.insert(patent)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增专利失败");
        }
        return result;
    }

    @Override
    public WebRestResult deletePatent(Patent patent) {
        WebRestResult result = new WebRestResult();
        if(patentMapper.deleteByPrimaryKey(patent.getPatentid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除专利失败");
        }
        return result;
    }

    @Override
    public WebRestResult updatePatent(Patent patent) {
        WebRestResult result = new WebRestResult();
        if(patentMapper.updateByPrimaryKeySelective(patent)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新专利失败");
        }
        return result;
    }

    @Override
    public PageResult<PatentPageResult> getPatentList(String year, int page, int pagesize) {
        PageResult<PatentPageResult> result = new PageResult<>();
        int start = (page-1) * pagesize;

        List<PatentPageResult> list = patentMapper.getPatentList(year,start,pagesize);
        if(list.size()>0){
            result.setPageCount(list.get(0).getPage());
            result.setTotal(list.get(0).getCnt());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }
}
